package com.xuexiang.xuidemo.page.uilts;

import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Component;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;

import com.xuexiang.xui_lib.util.ColorUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;

import java.util.Locale;

/**
 * ColorUtils示例页面
 *
 * @since 2021-03-23
 */
@Page(name = "ColorUtils", action = "ColorUtils")
public class ColorUtilsAbilitySlice extends BasePageAbilitySlice {
    private static final int PROGRESS_MAX = 100;
    private static final float FLOAT_ALPHA = 0.5F;
    private static final int INT_50 = 50;
    private Component tvRandomColor;
    private Text colorTip;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_utils_color;
    }

    @Override
    protected void onActive() {
        super.onActive();
        showSetColorAlpha();
        showComputeColor();
        showRandomColor();
        showTvColor();
    }

    /**
     * 将 color 颜色值转换为字符串
     */
    private void showTvColor() {
        final Text tvColorToString = findCastComponentById(ResourceTable.Id_tv_color_to_string);
        String transformColor = ColorUtils.colorToString(tvColorToString.getTextColor().getValue());
        tvColorToString.setText(String.format(Locale.ROOT, "这个 TextView 的字体颜色是：%1$s", transformColor));
    }

    /**
     * 设置颜色的 alpha 值; setColorAlpha
     */
    private void showSetColorAlpha() {
        Component originComponent = findComponentById(ResourceTable.Id_origin_alpha);
        Component resultComponent = findComponentById(ResourceTable.Id_result_alpha);

        int originAgbColor = RgbPalette.RED.asArgbInt();
        originComponent.setBackground(ColorUtils.getColorShapeElement(originAgbColor));

        int resultAlphaColor = ColorUtils.setColorAlpha(originAgbColor, FLOAT_ALPHA);
        resultComponent.setBackground(ColorUtils.getColorShapeElement(resultAlphaColor));
    }

    /**
     * 根据比例，在两个 color 值之间计算出一个 color 值
     */
    private void showComputeColor() {
        final Component startView = findComponentById(ResourceTable.Id_compute_color_1);
        final Component endView = findComponentById(ResourceTable.Id_compute_color_2);
        final Component computeResultView = findComponentById(ResourceTable.Id_compute_color_result);
        Slider sliderView = findCastComponentById(ResourceTable.Id_compute_color_slider);

        sliderView.setMaxValue(PROGRESS_MAX);
        sliderView.setMinValue(0);
        int startColor = RgbPalette.BLUE.asArgbInt();
        int endColor = RgbPalette.YELLOW.asArgbInt();
        startView.setBackground(ColorUtils.getColorShapeElement(startColor));
        endView.setBackground(ColorUtils.getColorShapeElement(endColor));
        sliderView.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean isFromUser) {
                int ratioColor = ColorUtils.computeColor(startColor, endColor, (float) progress / PROGRESS_MAX);
                computeResultView.setBackground(ColorUtils.getColorShapeElement(ratioColor));
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
        sliderView.setProgressValue(INT_50);
    }

    private void showRandomColor() {
        tvRandomColor = findComponentById(ResourceTable.Id_cp_get_random_color);
        colorTip = findCastComponentById(ResourceTable.Id_tv_is_drat);
        tvRandomColor.setClickedListener(component -> getRandomColor());
        getRandomColor();
    }

    /**
     * 获取随机颜色
     */
    private void getRandomColor() {
        int randomColor = ColorUtils.getRandomColor();
        tvRandomColor.setBackground(ColorUtils.getColorShapeElement(randomColor));
        colorTip.setText(String.format(Locale.ROOT, "颜色是否为深色:%s", ColorUtils.isColorDark(randomColor)));
    }
}
